package com.mikepenz.actionitembadge.library;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.TableLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import com.mikepenz.actionitembadge.library.utils.BadgeStyle;
import com.mikepenz.actionitembadge.library.utils.RecordStatus;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.typeface.IIcon;
import com.zyh.library.ResourceTable;

public class ActionItemBadgeAdder {
    /**
     * 构造器
     **/
    public ActionItemBadgeAdder() {
    }

    /**
     * 设置构造器
     *
     * @param ability
     * @param title
     **/
    public ActionItemBadgeAdder(AbilitySlice ability, String title) {
        this.abilitySlice = ability;
        this.title = title;
    }

    private AbilitySlice abilitySlice;

    /**
     * 获取当前ActionItemBadgeAdder对象
     *
     * @param ability
     * @return ActionItemBadgeAdder
     **/
    public ActionItemBadgeAdder act(AbilitySlice ability) {
        this.abilitySlice = ability;
        return this;
    }


    private DirectionalLayout menu;

    /**
     * 获取当前ActionItemBadgeAdder对象
     *
     * @param menu 需要加载组件的父控件
     * @return ActionItemBadgeAdder
     **/
    public ActionItemBadgeAdder menu(DirectionalLayout menu) {
        this.menu = menu;
        return this;
    }

    private String title;

    /**
     * 获取当前ActionItemBadgeAdder对象
     *
     * @param title 标题
     * @return ActionItemBadgeAdder
     **/
    public ActionItemBadgeAdder title(String title) {
        this.title = title;
        return this;
    }

    /**
     * 获取当前ActionItemBadgeAdder对象
     *
     * @param resId 需要加载组件的Id
     * @return ActionItemBadgeAdder
     **/
    public ActionItemBadgeAdder title(int resId) {
        if (abilitySlice == null) {
            throw new RuntimeException("Activity not set");
        }
        this.title = abilitySlice.getString(resId);
        return this;
    }

    private Integer groupId;
    private Integer itemId;
    private Integer order;

    /**
     * 获取当前ActionItemBadgeAdder对象
     *
     * @param groupId
     * @param itemId
     * @param order
     * @return ActionItemBadgeAdder
     **/
    public ActionItemBadgeAdder itemDetails(int groupId, int itemId, int order) {
        this.groupId = groupId;
        this.itemId = itemId;
        this.order = order;
        return this;
    }

    private Integer showAsAction;

    /**
     * 获取当前ActionItemBadgeAdder对象
     *
     * @param showAsAction
     * @return ActionItemBadgeAdder
     **/
    public ActionItemBadgeAdder showAsAction(int showAsAction) {
        this.showAsAction = showAsAction;
        return this;
    }

    /***
     * 添加数据
     * @param badgeCount badge数目
     * @return Component
     **/
    public Component add(int badgeCount) {
        return add(ActionItemBadge.BadgeStyles.GREY_LARGE, badgeCount);
    }

    /**
     * 更换
     *
     * @param style 特性
     * @param badgeCount badge数目
     * @return Component
     ***/
    public Component add(ActionItemBadge.BadgeStyles style, int badgeCount) {
        return add(style.getStyle(), badgeCount);
    }

    private boolean single = false;

    /**
     * 更新数据
     *
     * @param style 特性
     * @param badgeCount badge数目
     * @return Component
     **/
    public Component add(BadgeStyle style, int badgeCount) {
        single = true;
        return add((IconicsDrawable) null, style, badgeCount, null);
    }

    /*** 添加系统图库, 和数据
     * @param icon 更新系统图库
     * @param badgeCount badge数目
     * @return Component
     * **/
    public Component add(IIcon icon, int badgeCount) {
        return add(icon, Color.WHITE.getValue(), badgeCount);
    }

    /**
     * 添加系统图库, 和数据
     *
     * @param icon 更新系统图库
     * @param iconColor 给图库设置背景色
     * @param badgeCount badge数目
     * @return Component
     **/
    public Component add(IIcon icon, int iconColor, int badgeCount) {
        IconicsDrawable drawable = new IconicsDrawable(null);
        drawable.setColor(new Color(iconColor));
        return add(drawable, ActionItemBadge.BadgeStyles.GREY, badgeCount);
    }

    /**
     * 添加Iconsicsdrawable对象, 设置badgeCount
     *
     * @param icon 传过来的IconsicsDrawable对象
     * @param badgeCount badge数目
     * @return Component
     **/
    public Component add(IconicsDrawable icon, int badgeCount) {
        return add(icon, ActionItemBadge.BadgeStyles.GREY, badgeCount);
    }

    /**
     * 只更新数据
     *
     * @param icon 上下文
     * @param style 特性
     * @param badgeCount badge数目
     * @return Component
     **/
    public Component add(Context icon, ActionItemBadge.BadgeStyles style, int badgeCount) {
        IconicsDrawable drawable = new IconicsDrawable(null, abilitySlice.getContext());
        drawable.setColor(Color.WHITE);
        return add(drawable, style.getStyle(), badgeCount);
    }

    /**
     * 更新IconicsDrawable, style, badgeCount对象
     *
     * @param icon drawable对象
     * @param style 属性
     * @param badgeCount badge数目
     * @return Component
     ***/
    public Component add(IconicsDrawable icon, BadgeStyle style, int badgeCount) {
        return add(icon, style, badgeCount);
    }

    /***
     * 更新数据,  设置原来的背景
     * @param icon 上下文
     * @param iconColor icon颜色
     * @param style 特性
     * @param badgeCount badge数目
     * @return Component
     ***/
    public Component add(Context icon, int iconColor, BadgeStyle style, int badgeCount) {
        IconicsDrawable drawable = new IconicsDrawable(null, abilitySlice.getContext());
        drawable.setColor(new Color(iconColor));
        return add(drawable, style, badgeCount, null);
    }

    /**
     * 更新IconsicsDrawable对象, 设置style对象, 更新badgeCount
     *
     * @param icon drawable对象
     * @param style 特性
     * @param badgeCount badge数目
     * @return Component
     **/
    public Component add(IconicsDrawable icon, ActionItemBadge.BadgeStyles style, int badgeCount) {
        return add(icon, style.getStyle(), badgeCount, null);
    }

    private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x000110, "AIBA");

    private Component component;

    /***
     * 添加组件, 添加监听
     * @param icon  IconicsDrawable对象
     * @param style 特性
     * @param badgeCount badge数目
     * @param listener badge监听器
     * @return Component
     *
     ***/
    public Component add(IconicsDrawable icon, BadgeStyle style, int badgeCount, ActionItemBadge.ActionItemBadgeListener listener) {
        DirectionalLayout item = null;
        if (groupId != null && itemId != null && order != null) {
            item = menu;
            if (single == true) {
                HiLog.info(label, "  " + "AIBA_RecordStatus  " + RecordStatus.isSwitchAbility() + " " + RecordStatus.isHideCheck());
                component = LayoutScatter.getInstance(abilitySlice.getContext()).parse(ResourceTable.Layout_menu_action_item_badge_large, null, false);
                Component component001 = component.findComponentById(ResourceTable.Id_menu_badge);
                DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig();

                if (RecordStatus.isSwitchAbility() == true) {
                    config.width = AttrHelper.vp2px(20, abilitySlice.getContext());
                    config.height = AttrHelper.vp2px(20, abilitySlice.getContext());
                    config.setMarginLeft(AttrHelper.vp2px(10, abilitySlice.getContext()));
                    component001.setLayoutConfig(config);
                } else if (RecordStatus.isSwitchAbility() == false && RecordStatus.isHideCheck() == false) {
                    config.setMarginLeft(AttrHelper.vp2px(1, abilitySlice.getContext()));
                    config.setMarginTop(AttrHelper.vp2px(10, abilitySlice.getContext()));
                    component001.setLayoutConfig(config);
                } else if (RecordStatus.isSwitchAbility() == false && RecordStatus.isHideCheck() == true) {
                    config.setMarginLeft(AttrHelper.vp2px(0, abilitySlice.getContext()));
                    config.setMarginTop(AttrHelper.vp2px(20, abilitySlice.getContext()));
                    component001.setLayoutConfig(config);
                }
                item.addComponent(component);
            } else {
                component = LayoutScatter.getInstance(abilitySlice.getContext()).parse(ResourceTable.Layout_menu_action_item_badge, null, false);
                item.addComponent(component);
            }

        } else {
            component = LayoutScatter.getInstance(abilitySlice.getContext()).parse(ResourceTable.Layout_menu_action_item_badge_large, null, false);

            Component component001 = component.findComponentById(ResourceTable.Id_menu_badge);
            DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig();

            if (RecordStatus.isSwitchAbility() == true) {
                config.width = AttrHelper.vp2px(20, abilitySlice.getContext());
                config.height = AttrHelper.vp2px(20, abilitySlice.getContext());
                config.setMarginLeft(AttrHelper.vp2px(5, abilitySlice.getContext()));
                component001.setLayoutConfig(config);
            } else {
                config.setMarginLeft(AttrHelper.vp2px(1, abilitySlice.getContext()));
                config.setMarginTop(AttrHelper.vp2px(5, abilitySlice.getContext()));
                component001.setLayoutConfig(config);
            }

            item.addComponent(component);
        }
        if (showAsAction != null) {

        }
        ActionItemBadge.update(abilitySlice, component, icon, style, badgeCount, listener);
        return item;
    }
}